package dot;

import java.io.File;
import java.rmi.ConnectException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import java.util.List;

import p2p.Config;
import p2p.Peer;
import server.Message;

public class Dot {
	public String getDot(List<Peer> peers) throws RemoteException, NotBoundException {
		Registry registry;
		Message rmiMessage;
		List<Peer> neighbors = null;
		List<Connection> connections = new ArrayList<Connection>();
		String res = "graph network {\n";
		
		for(Peer p : peers) {
			//Get neighborlist for peer p
			registry = LocateRegistry.getRegistry(p.getIp(), p.getPort()); //get the “registry” 
			rmiMessage = (Message)(registry.lookup(Config.ServerName)); //look up remote object
			neighbors = rmiMessage.getNeighbors(); //call remote method

			for(Peer n : neighbors) {
				//edge
				if(!connections.contains(new Connection(p.getName(), n.getName()))) { //does edge already exist?
					res += "\""+p.getName()+"("+p.getMaxNumberOfNeighbours()+")("+p.getCapacity()+")\" -- " + "\""+n.getName()+"("+n.getMaxNumberOfNeighbours()+")("+n.getCapacity()+")\";\n";
					connections.add(new Connection(p.getName(), n.getName()));
				}
			}
		}
		
		return res + "}";
	}
	
	public boolean saveToFile(File f) {
		return false;
	}
}
